from django.http import HttpResponseRedirect,Http404 #网页重定向
from django.core.urlresolvers import reverse #一个把视图函数转换成url的函数
from django.shortcuts import render
from django.contrib.auth.decorators import login_required

from learning_logs.models import Topic,Entry,Comment
from learning_logs.forms import TopicForm,EntryForm,CommentForm

# Create your views here.

def index(request):
    return render(request,"learning_logs/index.html") 

@login_required
def topics(request):
    topics = Topic.objects.filter(ower=request.user).order_by('date_added')
    msg = ""
    if len(topics)==0:
        msg = "你还没创建任何主题"
    context = {"topics":topics,'msg':msg}
    return render(request,"learning_logs/topic_lists.html",context)

@login_required
def topic(request,id):

    topic = Topic.objects.get(id=id)
    if topic.ower!=request.user:
        return Http404

    entries = topic.entry_set.all() #在Entry表设置了一个外键是topic 所有在topic可以通过 entry_set(自动生成的属性) 获取所有entry
    context = {"topic":topic,"entries":entries}
    
    return render(request,"learning_logs/topic_inf.html",context)

#这里是处理表单的例子
@login_required
def new_topic(request):
    ''' 添加新的主题 '''

    if request.method != "POST":
        form = TopicForm()
    else:
        form = TopicForm(request.POST)
        if form.is_valid():
            new_topic = form.save()
            new_topic.ower = request.user #保存发布主题的用户信息
            new_topic.save()

            return HttpResponseRedirect(reverse("learning_logs:topics"))
    context = {"form":form}
    return render(request,"learning_logs/new_topic.html",context=context)
@login_required
def new_entry(request,id):
    ''' 添加新的条目 '''   
    topic = Topic.objects.get(id=id)
    if request.method != "POST":
        form = EntryForm()
    else:
        form = EntryForm(data=request.POST)        
        if form.is_valid():
            new_entry = form.save(commit=False) #表单返回的是对应定义的model实例
            new_entry.topic = topic #外键关联的实例
            new_entry.save()          
            return HttpResponseRedirect(reverse("learning_logs:topic",args=[id])) #重定向传参数

    context = {"form":form,"topic":topic}
    return render(request,"learning_logs/new_entry.html",context=context)
@login_required
def edit_entry(request,id):
    '''编辑条目'''

    entry = Entry.objects.get(id=id)
    topic = entry.topic
    if request.method != "POST":
        form = EntryForm(instance=entry)
    else:
        form = EntryForm(instance=entry,data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse("learning_logs:topic",args=[topic.id]))

    context = {"entry":entry,"topic":topic,"form":form}
    return render(request,"learning_logs/edit_entry.html",context=context)

@login_required
def comment(request,entry_id):
    entry = Entry.objects.get(id=entry_id)
    comments = entry.comment_set.all()
    context = {"comments":comments,'entry':entry,"topic":entry.topic}
    return render(request,'learning_logs/comment_list.html',context=context)

@login_required
def add_comment(request,id):
    entry = Entry.objects.get(id=id)
    if request.method != "POST":
        form = CommentForm()
    else:
        form = CommentForm(data=request.POST)
        if form.is_valid():           
            new_commonent = form.save(commit=False) #这里不能直接提交到数据库 下面还需要设置数据
            new_commonent.ower = request.user             
            new_commonent.entry = entry
            new_commonent.save()            
            return HttpResponseRedirect(reverse("learning_logs:topic",args=[entry.topic.id]))
    context = {"form":form,'entry':entry,"topic":entry.topic}
    return render(request,"learning_logs/add_comment.html",context=context)